<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box1 {
            width: 500px;
            height: 500px;
            background-color: pink;
            margin: 0 auto;
        }
        #box2 {
            width: 300px;
            height: 300px;
            background-color: skyblue;
        }
        #box3 {
            width: 100px;
            height: 100px;
            background-color: green;
        }
    </style>
</head>
<body>
    <div id="box1">box1
        <div id="box2">box2
            <div id="box3">box3</div>
        </div>
    </div>
    <script>
        // 获取元素
        var box1 = document.getElementById("box1")
        var box2 = document.getElementById("box2")
        var box3 = document.getElementById("box3")
        // 绑定到捕获阶段
        box1.attachEvent("onclick", function () {
            console.log("box1");
        });

        box2.attachEvent("onclick", function (e) {
            // 阻止冒泡
            e.cancelBubble = true;
            console.log("box2");
        });

        box3.attachEvent("onclick", function (e) {
            // 阻止冒泡
            e.cancelBubble = true;
            console.log("box3");
        });
    </script>
</body>
</html>